﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class ConsumerCouponDAL : BaseDAL<ConsumerCouponDAL>
	{
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ConsumerCoupon model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into ConsumerCoupon(");
            strSql.Append("ConsumerId,CouponId,GetTime,IsGetFromSubscribe,EnableCouponWithDraw,IsDelete");
            strSql.Append(") values (");
            strSql.Append("@ConsumerId,@CouponId,@GetTime,@IsGetFromSubscribe,@EnableCouponWithDraw,@IsDelete");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CouponId", SqlDbType.Int,4) ,            
                        new SqlParameter("@GetTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsGetFromSubscribe", SqlDbType.Bit,1) ,            
                        new SqlParameter("@EnableCouponWithDraw", SqlDbType.Bit,1) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.ConsumerId;
            parameters[1].Value = model.CouponId;
            parameters[2].Value = model.GetTime;
            parameters[3].Value = model.IsGetFromSubscribe;
            parameters[4].Value = model.EnableCouponWithDraw;
            parameters[5].Value = model.IsDelete; var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ConsumerCoupon model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ConsumerCoupon set ");

            strSql.Append(" ConsumerId = @ConsumerId , ");
            strSql.Append(" CouponId = @CouponId , ");
            strSql.Append(" GetTime = @GetTime , ");
            strSql.Append(" IsGetFromSubscribe = @IsGetFromSubscribe , ");
            strSql.Append(" EnableCouponWithDraw = @EnableCouponWithDraw , ");
            strSql.Append(" IsDelete = @IsDelete  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CouponId", SqlDbType.Int,4) ,            
                        new SqlParameter("@GetTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsGetFromSubscribe", SqlDbType.Bit,1) ,            
                        new SqlParameter("@EnableCouponWithDraw", SqlDbType.Bit,1) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.ConsumerId;
            parameters[2].Value = model.CouponId;
            parameters[3].Value = model.GetTime;
            parameters[4].Value = model.IsGetFromSubscribe;
            parameters[5].Value = model.EnableCouponWithDraw;
            parameters[6].Value = model.IsDelete; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ConsumerCoupon set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ConsumerCoupon set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, ConsumerId, CouponId, GetTime, IsGetFromSubscribe, EnableCouponWithDraw, IsDelete  ");
            strSql.Append("  from ConsumerCoupon ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ConsumerCoupon where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
		
		#region 扩展方法

        /// <summary>
        /// 查询用户是否存在第一次关注获取的优惠券记录
        /// </summary>
        /// <param name="consumerId"></param>
        /// <returns></returns>
	    public int CheckConsumerCouponSubscribe(int consumerId)
	    {
            var strSql = new StringBuilder();
            strSql.Append("SELECT COUNT(Id) FROM ConsumerCoupon ");
            strSql.Append("WHERE ConsumerId=@ConsumerId  and IsGetFromSubscribe=1");
            SqlParameter[] parameters = {
					new SqlParameter("@ConsumerId", SqlDbType.Int,4)
			};
            parameters[0].Value = consumerId;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
	        return obj != null ? (int) obj : 0;
	    }

        /// <summary>
        /// 更新用户未使用的优惠券可提现字段值
        /// </summary>
        /// <param name="consumerId"></param>
        /// <param name="isWithDraw"></param>
        /// <returns></returns>
        public bool ConvertWithDrawValueOfUnusedConsumerCoupon(int consumerId, bool isWithDraw)
	    {
            var strSql = new StringBuilder();
            strSql.Append("UPDATE ConsumerCoupon SET EnableCouponWithDraw=@EnableCouponWithDraw  ");
            strSql.Append("WHERE ConsumerId=@ConsumerId  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@ConsumerId", SqlDbType.Int,4),
                    new SqlParameter("@EnableCouponWithDraw", SqlDbType.Int,4)
			};
            parameters[0].Value = consumerId;
            parameters[1].Value = isWithDraw ? 1 : 0;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
	    }

	    /// <summary>
	    /// 更新用户指定的优惠券可提现字段值
	    /// </summary>
	    /// <param name="consumerCouponId"></param>
	    /// <param name="isWithDraw"></param>
	    /// <param name="connection"></param>
	    /// <param name="sqlTrans"></param>
	    /// <returns></returns>
	    public bool ConvertWithDrawValueByConsumerIdAndCouponId(int consumerCouponId, bool isWithDraw,
	        SqlConnection connection = null, SqlTransaction sqlTrans = null)
        {
            var strSql = new StringBuilder();
            strSql.Append("UPDATE ConsumerCoupon SET EnableCouponWithDraw=@EnableCouponWithDraw  ");
            strSql.Append(" WHERE Id=@ConsumerCouponId");
            SqlParameter[] parameters = {
				new SqlParameter("@ConsumerCouponId", SqlDbType.Int,4),
                new SqlParameter("@EnableCouponWithDraw", SqlDbType.Bit,1)
			};
            parameters[0].Value = consumerCouponId;
            parameters[1].Value = isWithDraw;

	        if (connection == null || sqlTrans == null)
	        {
                return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
	        }
	        return DbHelperSQL.ExecuteSql(connection, sqlTrans, strSql.ToString(), parameters) > 0;
        }

		#endregion
	}
}
